package com.ruoyi.maode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.exception.UtilException;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.maode.domain.dto.QueryPartBatchDTO;
import com.ruoyi.maode.domain.entity.PartBatch;
import com.ruoyi.maode.domain.entity.PartMeasure;
import com.ruoyi.maode.domain.entity.PartReport;
import com.ruoyi.maode.domain.entity.Supplier;
import com.ruoyi.maode.domain.vo.PartBatchDetailVO;
import com.ruoyi.maode.domain.vo.PartBatchVO;
import com.ruoyi.maode.mapper.PartBatchMapper;
import com.ruoyi.maode.mapper.PartReportMapper;
import com.ruoyi.maode.service.PartBatchService;
import com.ruoyi.maode.service.PartMeasureService;
import com.ruoyi.maode.service.SupplierService;
import com.ruoyi.web.core.config.ResultDTO;
import com.ruoyi.web.util.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * 零件批次接口实现类
 * @author zhangwenqiang
 */
@Service
public class PartBatchServiceImpl extends ServiceImpl<PartBatchMapper, PartBatch> implements PartBatchService {

    @Autowired
    private PartBatchMapper partBatchMapper;

    @Autowired
    private PartReportMapper partReportMapper;

    @Autowired
    private PartMeasureService partMeasureService;

    @Autowired
    private SupplierService supplierService;

    @Value("${server.url}")
    private String serverUrl;

    @Override
    public ResultDTO add(PartBatch partBatch) {
        // 判断批次号是否重复
        PartBatch one = baseMapper.selectOne(new LambdaQueryWrapper<PartBatch>()
                .eq(PartBatch::getBatchCode, partBatch.getBatchCode())
                .eq(PartBatch::getPartId,partBatch.getPartId()));
        if (one != null) {
            throw new UtilException("批次号已存在，请确认！");
        }

        int insert = baseMapper.insert(partBatch);
        return insert > 0 ? ResultDTO.success() : ResultDTO.error("新增失败！");
    }

    @Override
    public Page<PartBatchVO> selectPartBatchList(PageQuery<QueryPartBatchDTO> dto) {
        Page page = new Page<PartBatchVO>(dto.getPage(), dto.getPageSize());
        QueryPartBatchDTO queryPartBatchDTO = dto.getData();
        Page<PartBatchVO> partBatchVOPage = partBatchMapper.selectBatchList(page, queryPartBatchDTO);
        partBatchVOPage.getRecords().stream().forEach(t->{
            PartReport partReport = partReportMapper.selectOne(new LambdaQueryWrapper<PartReport>()
                    .eq(PartReport::getBatchId, t.getId())
                    .eq(PartReport::getPartId,t.getPartId()));
            // 查询毛坯厂家
            Supplier supplier = supplierService.getOne(new LambdaQueryWrapper<Supplier>()
                    .eq(Supplier::getId, t.getRoughSupplierId()));
            if (supplier != null) {
                t.setRoughSupplierName(supplier.getSupplierName());
            }
            if (partReport != null) {
                t.setIsApprove(1);
            } else {
                t.setIsApprove(0);
            }
        });
        return partBatchVOPage;
    }

    @Override
    public PartBatchDetailVO getDetail(Long id) {
        PartBatchDetailVO detail = partBatchMapper.getDetail(id);
        // 查询毛坯厂家
        Supplier supplier = supplierService.getOne(new LambdaQueryWrapper<Supplier>()
                .eq(Supplier::getId, detail.getRoughSupplierId()));
        if (supplier != null) {
            detail.setRoughSupplierName(supplier.getSupplierName());
        }

        return detail;
    }

    public  String getMaoDeIp() {
        return "http://" + IpUtils.getPPPoEIP() + serverUrl;
    }
}
